package model.dba;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.IRHorarioCursada;
import model.IRHorarioInstructor;

public class IRHorarioInstructorDAO extends JDBC{
	private static IRHorarioInstructorDAO iRHorarioInstructorDAO;

	private IRHorarioInstructorDAO() {
		super();
	}

	public static IRHorarioInstructorDAO getInstance() {
		if (iRHorarioInstructorDAO == null) {
			iRHorarioInstructorDAO = new IRHorarioInstructorDAO();
		}
		return iRHorarioInstructorDAO;
	}

	public int add(IRHorarioInstructor iRHorarioInstructor) {
		try {

			String queryString = "INSERT INTO iRhorarioInstructor(idHorarioC,dniInstructorC)" + " VALUES(?,?) ";
			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setLong(1, iRHorarioInstructor.getIdHorario());
			preparedStatement.setString(2,iRHorarioInstructor.getdniInstrctor());

			preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return getLastId();

	}

	// En un futuro se puede poner nombre

	public boolean update(IRHorarioInstructor iRHorarioInstructor) {
		boolean state = false;
		try {
			String queryString = "UPDATE iRhorarioInstructor SET idHorarioC=?, dniInstructorC=? WHERE idHorarioC=?";

			preparedStatement = connection.prepareStatement(queryString);

			preparedStatement.setLong(1, iRHorarioInstructor.getIdHorario());
			preparedStatement.setString(2, iRHorarioInstructor.getdniInstrctor());
			preparedStatement.setLong(3, iRHorarioInstructor.getIdHorario());


			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

	public boolean delete(Integer idHorarioC) {
		boolean state = false;
		try {
			String queryString = "DELETE FROM iRhorarioInstructor WHERE idHorarioC=?";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setInt(1, idHorarioC);
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return state;

	}

	public List<IRHorarioInstructor> findAll(String dniInstructor) {

		List<IRHorarioInstructor> areaList = new ArrayList<IRHorarioInstructor>();
		try {
			String queryString = "SELECT * FROM iRhorarioInstructor where dniInstructorC = '" + dniInstructor + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				areaList.add(populate());

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return areaList;
	}

	
	public IRHorarioInstructor get(Integer idHorario) {

		IRHorarioInstructor iRHorarioInstructor = new IRHorarioInstructor();
		try {
			String queryString = "SELECT * FROM iRHorarioInstructor where idHorarioC = '" + idHorario + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				iRHorarioInstructor = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRHorarioInstructor;
	}

	// En un futuro se puede poner nombre

	public IRHorarioInstructor find(String dniInstructor) {

		IRHorarioInstructor iRHorarioInstructor = new IRHorarioInstructor();
		try {
			String queryString = "SELECT * FROM iRhorarioInstructor where dniInstructorC ='" + dniInstructor + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				iRHorarioInstructor = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRHorarioInstructor;
	}

	public int getLastId() {
		int ret = 0;
		try {
			String queryString = "SELECT idHorarioC FROM iRHorarioInstructor order by idHorarioC DESC LIMIT 1  ";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ret = resultSet.getInt("idHorarioC");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return ret;

	}

	public List<IRHorarioInstructor> getAll() {
		List<IRHorarioInstructor> iRHorarioInstructorList = new ArrayList<IRHorarioInstructor>();

		try {
			String queryString = "SELECT * FROM iRHorarioInstructor ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				iRHorarioInstructorList.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRHorarioInstructorList;
	}

	private IRHorarioInstructor populate() throws SQLException {

		IRHorarioInstructor iRHorarioInstructor = new IRHorarioInstructor();
		iRHorarioInstructor.setIdhorario((int) resultSet.getLong("idHorarioC"));
		iRHorarioInstructor.setdniIntructor(resultSet.getString("dniInstructorC"));
		return iRHorarioInstructor;

	}

}
